﻿using System;
using System.Data;
using System.Drawing;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using HiauPdm.Bll;
using HiauPdm.Web;

namespace HiauPdm.Web
{
    public partial class CpSjLbjBj : PageBase
    {
        protected override bool IsAccess()
        {
            return IsAccess("CpSjBj.aspx");
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Page.MaintainScrollPositionOnPostBack = true;
            if (!Page.IsPostBack)
            {
                TextId.Value = Request.QueryString["CpSjId"];
                BtnSave.OnClientClick = "this.disabled=true;" + Page.ClientScript.GetPostBackEventReference(BtnSave, null) + ";return false;";
                BindData();
                TextMc.Focus();
            }
        }

        private void BindData()
        {
            if ((TextId.Value.Trim() != string.Empty) && BGlobal.IsNumeric(TextId.Value.Trim()))
            {
                DataSet ds = BCpSjBj.GetCpSjBjBySjId(Convert.ToInt32(TextId.Value.Trim()));
                if ((ds != null) && (ds.Tables[0].Rows.Count == 1))
                {
                    PanelEdit.Visible = true;
                    BtnSave.Enabled = true;
                    TextDh.Text = ds.Tables[0].Rows[0]["代号"].ToString();
                    TextLb.Text = ds.Tables[0].Rows[0]["类别"].ToString();
                    TextMc.Text = ds.Tables[0].Rows[0]["名称"].ToString();
                    TextGgxh.Text = ds.Tables[0].Rows[0]["规格型号"].ToString();
                    TextDw.Text = ds.Tables[0].Rows[0]["单位"].ToString();
                    TextBz.Text = ds.Tables[0].Rows[0]["备注"].ToString();
                    TextDjr.Text = ds.Tables[0].Rows[0]["登记人"].ToString();
                    TextBjr.Text = ds.Tables[0].Rows[0]["编辑人"].ToString();
                    TextBjsj.Text = ds.Tables[0].Rows[0]["编辑时间"].ToString();
                    if (TextDjr.Text != (string)Session["RealName"] && !IsAccess("编辑他人的产品设计"))
                    {
                        TextMc.ReadOnly = true;
                        TextGgxh.ReadOnly = true;
                        TextDw.ReadOnly = true;
                        TextBz.ReadOnly = true;
                        TextMc.BackColor = Color.FromArgb(0xE7F3FF);
                        TextGgxh.BackColor = Color.FromArgb(0xE7F3FF);
                        TextDw.BackColor = Color.FromArgb(0xE7F3FF);
                        TextBz.BackColor = Color.FromArgb(0xE7F3FF);
                    }
                    if (((int)ds.Tables[0].Rows[0]["大类"]) == 0)
                    {
                        Button2.Visible = Button3.Visible = false;
                        BtnSave.Enabled = false;
                        return; // 不存在零部件编辑
                    }
                    else if (((int)ds.Tables[0].Rows[0]["大类"]) == 1)
                    {
                        Button3.Visible = false;
                    }
                    BindCpSjLbjData(Convert.ToInt32(TextId.Value.Trim()));
                }
            }
        }

        private void BindCpSjLbjData(int iCpSjId)
        {
            DataSet ds = BCpSjBj.GetCpSjLbjBjBySjId(iCpSjId);
            if ((ds != null) && (ds.Tables[0].Rows.Count > 0))
            {
                for (int i = 0; i < (GridCpSjLbj.DataKeys.Count - 1); i++)
                {
                    ds.Tables[0].DefaultView.RowFilter = "ID=" + GridCpSjLbj.DataKeys[i].Value.ToString();
                    if (ds.Tables[0].DefaultView.Count == 1)
                    {
                        if (BGlobal.IsNumeric(((TextBox)GridCpSjLbj.Rows[i].Cells[5].Controls[1]).Text) && (int)ds.Tables[0].DefaultView[0]["装入数量"] != Convert.ToInt32(((TextBox)GridCpSjLbj.Rows[i].Cells[5].Controls[1]).Text))
                        {
                            ds.Tables[0].DefaultView[0]["装入数量"] = Convert.ToInt32(((TextBox)GridCpSjLbj.Rows[i].Cells[5].Controls[1]).Text);
                            ds.Tables[0].DefaultView[0]["编辑标志"] = true;
                        }
                        if ((string)ds.Tables[0].DefaultView[0]["装入代号"] != ((TextBox)GridCpSjLbj.Rows[i].Cells[6].Controls[1]).Text)
                        {
                            ds.Tables[0].DefaultView[0]["装入代号"] = ((TextBox)GridCpSjLbj.Rows[i].Cells[6].Controls[1]).Text;
                            ds.Tables[0].DefaultView[0]["编辑标志"] = true;
                        }
                    }
                }
                ds.Tables[0].DefaultView.RowFilter = string.Empty;
            }
            if (ds != null)
            {
                ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            }
            GridCpSjLbj.DataSource = ds;
            GridCpSjLbj.DataBind();
            GridCpSjLbj.SelectedIndex = -1;
            SetCzQx();
        }

        private void SetCzQx()
        {
            for (int i = 0; i < (GridCpSjLbj.DataKeys.Count - 1); i++)
            {
                if ((bool)GridCpSjLbj.DataKeys[i].Values[3])
                {
                    ((TextBox)GridCpSjLbj.Rows[i].Cells[5].Controls[1]).ForeColor = Color.Red;
                    ((TextBox)GridCpSjLbj.Rows[i].Cells[6].Controls[1]).ForeColor = Color.Red;
                }
                if (GridCpSjLbj.DataKeys[i].Values[1].ToString() != "未提交")
                {
                    ((HyperLink)GridCpSjLbj.Rows[i].Cells[1].Controls[1]).NavigateUrl = ((HyperLink)GridCpSjLbj.Rows[i].Cells[1].Controls[1]).NavigateUrl.Replace("CpSjEdit", "CpSjBrowse");
                    ((HyperLink)GridCpSjLbj.Rows[i].Cells[1].Controls[1]).ToolTip = ((HyperLink)GridCpSjLbj.Rows[i].Cells[1].Controls[1]).ToolTip.Replace("编辑", "查看");
                }
            }
            if (TextDjr.Text != (string)Session["RealName"] && !IsAccess("编辑他人的产品设计"))
            {
                for (int i = 0; i < (GridCpSjLbj.Rows.Count - 1); i++)
                {
                    if ((string)GridCpSjLbj.DataKeys[i].Values[2] != (string)Session["RealName"])
                    {
                        ((TextBox)GridCpSjLbj.Rows[i].Cells[5].Controls[1]).ReadOnly = true;
                        ((TextBox)GridCpSjLbj.Rows[i].Cells[6].Controls[1]).ReadOnly = true;
                        GridCpSjLbj.Rows[i].Cells[9].Controls[1].Visible = false;
                        GridCpSjLbj.Rows[i].Cells[9].Controls[3].Visible = false;
                    }
                }
            }
        }

        protected void BtnSave_Click(object sender, EventArgs e)
        {
            if (!ShowErr(BCpSjBj.SaveCpSjLbj(Convert.ToInt32(TextId.Value.Trim()), TextMc.Text, TextGgxh.Text, TextDw.Text, TextBz.Text, (string)Session["RealName"], GridCpSjLbj)))
            {
                Response.Redirect(Request.Url.PathAndQuery);
            }
        }

        protected void GridCpSjLbj_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if ((e.Row.RowType == DataControlRowType.DataRow) && (GridCpSjLbj.DataKeys[e.Row.DataItemIndex].Value.ToString() == string.Empty))
            {
                e.Row.Cells[0].Controls[1].Visible = false;
                e.Row.Cells[1].Controls[1].Visible = false;
                e.Row.Cells[5].Controls[1].Visible = false;
                e.Row.Cells[6].Controls[1].Visible = false;
                e.Row.Cells[9].Controls[3].Visible = false;
                ((LinkButton)e.Row.Cells[9].Controls[1]).Text = "添加";
                ((LinkButton)e.Row.Cells[9].Controls[1]).ToolTip = "添加零部件";
                ((LinkButton)e.Row.Cells[9].Controls[1]).CommandName = "AddCpSjLbj";
            }
        }

        protected void GridCpSjLbj_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "AddCpSjLbj")
            {
                if (!ShowErr(BCpSjBj.SaveCpSjLbjAdd(Convert.ToInt32(TextId.Value.Trim()), HfCpSjLbjAdd.Value.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries), (string)Session["RealName"])))
                {
                    BindCpSjLbjData(Convert.ToInt32(TextId.Value.Trim()));
                }
            }
            else if (e.CommandName == "ThCpSjLbj")
            {
                string[] sDhArr = HfCpSjLbjAdd.Value.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                if (!ShowErr(BCpSjBj.SaveCpSjLbjTh(Convert.ToInt32(TextId.Value.Trim()), (int)GridCpSjLbj.DataKeys[Convert.ToInt32(e.CommandArgument)].Value, sDhArr[sDhArr.Length - 1], (string)Session["RealName"])))
                {
                    int iSelectId = Convert.ToInt32(GridCpSjLbj.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
                    BindCpSjLbjData(Convert.ToInt32(TextId.Value.Trim()));
                    for (int idx = 0; idx < GridCpSjLbj.DataKeys.Count; idx++)
                    {
                        if ((int)GridCpSjLbj.DataKeys[idx].Value == iSelectId)
                        {
                            GridCpSjLbj.SelectedIndex = idx;
                            break;
                        }
                    }
                }
            }
            else if (e.CommandName == "DelCpSjLbj")
            {
                if (!ShowErr(BCpSjBj.DelCpSjLbj((int)GridCpSjLbj.DataKeys[Convert.ToInt32(e.CommandArgument)].Value)))
                {
                    BindCpSjLbjData(Convert.ToInt32(TextId.Value.Trim()));
                    GridCpSjLbj.SelectedIndex = (Convert.ToInt32(e.CommandArgument) < GridCpSjLbj.Rows.Count - 1 ? Convert.ToInt32(e.CommandArgument) : GridCpSjLbj.Rows.Count - 2);
                }
            }
        }
    }
}